package com.sdstc.modules.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.PersonStatus;
import com.sdstc.modules.dao.OrgDao;
import com.sdstc.modules.dao.OrgDetailYearDao;
import com.sdstc.modules.eneity.OrgDetailYear;
import com.sdstc.modules.eneity.OrgEntity;
import com.sdstc.modules.service.OrgDetailYearService;
import com.sdstc.modules.service.OrgService;
import com.sdstc.modules.vo.BarEchartsVo;
import com.sdstc.modules.vo.InstitutionDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 机构年份详情Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-11
 */
@Service
public class OrgDetailYearServiceImpl extends ServiceImpl<OrgDetailYearDao, OrgDetailYear> implements OrgDetailYearService
{

    @Autowired
    private OrgDetailYearDao orgDetailYearDao;
    @Autowired
    private OrgDao orgDao;


    @Override
    public BarEchartsVo queryBarEchartsData(String orgId) {
        List<OrgDetailYear> orgDetailYears = orgDetailYearDao.queryBarEchartsData(orgId);
        BarEchartsVo barEchartsVo = new BarEchartsVo();
        for (OrgDetailYear orgDetailYear : orgDetailYears) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
            barEchartsVo.getYearList().add(simpleDateFormat.format(orgDetailYear.getCreateTime()));
            barEchartsVo.getAdminDataList().add(orgDetailYear.getAdmin());
            barEchartsVo.getSingleDataList().add(orgDetailYear.getSingle());
            barEchartsVo.getWorkDataList().add(orgDetailYear.getWork());
            barEchartsVo.getCauseDataList().add(orgDetailYear.getCause());
            barEchartsVo.getAdminPersonDataList().add(orgDetailYear.getAdminPerson());
            barEchartsVo.getSinglePersonDataList().add(orgDetailYear.getSinglePerson());
            barEchartsVo.getWorkPersonDataList().add(orgDetailYear.getWorkPerson());
            barEchartsVo.getCausePersonDataList().add(orgDetailYear.getCausePerson());
        }
        return barEchartsVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveYearData() {
        OrgDetailYear orgDetailYear = new OrgDetailYear();
        OrgEntity org = orgDao.selectById("1584019929357635586");
        orgDetailYear.setOrgId(org.getId());
        orgDetailYear.setAdmin(org.getOrganizationAdmin());
        orgDetailYear.setSingle(org.getOrganizationSingle());
        orgDetailYear.setWork(org.getOrganizationWork());
        orgDetailYear.setCause(org.getOrganizationCause());
        orgDetailYear.setCreateTime(new Date());
        InstitutionDetailVo orgCateCount = getOrgCateCount(org);
        orgDetailYear.setAdminPerson(orgCateCount.getOrgAdminNow());
        orgDetailYear.setSinglePerson(orgCateCount.getOrgSingleNow());
        orgDetailYear.setWorkPerson(orgCateCount.getOrgWorkNow());
        orgDetailYear.setCausePerson(orgCateCount.getOrgCauseNow());
        orgDetailYearDao.insert(orgDetailYear);
    }

    //获取机构和下属机构的职务人数（没有人员调整，统计图用）
    public InstitutionDetailVo getOrgCateCount(OrgEntity orgEntity){
        //当前所选机构的人员
        InstitutionDetailVo institutionDetailVo = orgDao.queryPostCateCount(orgEntity.getId(), PersonStatus.OK.getCode(), PersonStatus.HIGH.getCode());
        int orgAdminNow = institutionDetailVo.getOrgAdminNow();
        int orgSingleNow = institutionDetailVo.getOrgSingleNow();
        int orgCauseNow = institutionDetailVo.getOrgCauseNow();
        int orgWorkNow = institutionDetailVo.getOrgWorkNow();
        List<OrgEntity> childOrgs = orgDao.selectList(new QueryWrapper<OrgEntity>().eq("pid",orgEntity.getId()));
        if (CollectionUtils.isEmpty(childOrgs)){
            return institutionDetailVo;
        }else {//递归
            for (OrgEntity childOrg : childOrgs) {
                InstitutionDetailVo childOrgCateCount = getOrgCateCount(childOrg);
                orgAdminNow = orgAdminNow + childOrgCateCount.getOrgAdminNow();
                orgSingleNow = orgSingleNow + childOrgCateCount.getOrgSingleNow();
                orgCauseNow = orgCauseNow + childOrgCateCount.getOrgCauseNow();
                orgWorkNow = orgWorkNow + childOrgCateCount.getOrgWorkNow();
            }
            institutionDetailVo.setOrgAdminNow(orgAdminNow);
            institutionDetailVo.setOrgSingleNow(orgSingleNow);
            institutionDetailVo.setOrgCauseNow(orgCauseNow);
            institutionDetailVo.setOrgWorkNow(orgWorkNow);
        }
        return institutionDetailVo;
    }

}
